package quota

import (
	"context"
	"fmt"

	"go-toolkit/library/net/metadata"

	"google.golang.org/grpc"
)

type Rule interface {
	// 规则名称
	Name() string
	// 返回的key
	Key(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo) string
}

type RuleTotal struct{}

func (r *RuleTotal) Name() string {
	return "total"
}

func (r *RuleTotal) Key(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo) string {
	return "total"
}

type RuleCaller struct{}

func (r *RuleCaller) Name() string {
	return "caller"
}

func (r *RuleCaller) Key(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo) string {
	caller := metadata.String(ctx, metadata.Caller)
	if caller == "" {
		caller = "blank"
	}
	return fmt.Sprintf("caller:%s", caller)
}
